﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace PMS.Controllers.BD
{
    public class ReviewController : Controller
    {
        //
        // GET: /ReviewType/

        public ActionResult ReviewType()
        {
            return View();
        }
        public ActionResult ReviewLow()
        {
            return View();
        }
        public ActionResult ReviewTotal()
        {
            return View();
        }
        /// <summary>
        /// 获取审批类型列表
        /// </summary>
        public void GetReviewTypeList()
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
            }
            Expression<Func<View_BD_RevDocInfo, bool>> exp = SearchExpression.FindByGroup<View_BD_RevDocInfo>(listSearch);
            int count = 0;
            var list = BLLSession.Current.View_BD_RevDocInfoBLL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, u => u.ID, OperateContext.Order).ToList();
            count = BLLSession.Current.View_BD_RevDocInfoBLL.GetCount(exp);
            Response.Write(JsonHelper.toPageJson(list, count));
        }
        /// <summary>
        /// 添加审批类型
        /// </summary>
        public void AddReviewType()
        {
            BD_RevDocInfo reInfo = new BD_RevDocInfo();
            ModelTools.Form2EditModel(Request.Form, reInfo);
            int count = BLLSession.Current.BD_RevDocInfoBLL.GetCount(r => r.ModName == reInfo.ModName);
            if (count == 0)
            {
                reInfo.DocCode = reInfo.ModName.PadLeft(2, '0') + "01";
            }
            else
            {
                reInfo.DocCode = reInfo.ModName.PadLeft(2, '0') + (count + 1).ToString().PadLeft(2, '0');
            }
            reInfo.FillDate = DateTime.Now.ToString("yyyy:MM:dd,HH:mm:ss");
            reInfo.ManClerk = OperateContext.sessUserInfo.Name;
            BLLSession.Current.BD_RevDocInfoBLL.Add(reInfo);
            Response.Write(JsonHelper.toDefaultMsg(true, "添加成功！"));
        }



        /// <summary>
        /// 修改审批类型
        /// </summary>
        public void EditReviewType()
        {
            int id = Commons.String2Int32(Request.Form["ID"]);
            BD_RevDocInfo reInfo = BLLSession.Current.BD_RevDocInfoBLL.GetListBy(r => r.ID == id).FirstOrDefault();
            ModelTools.Form2EditModel(Request.Form, reInfo);

            reInfo.FillDate = DateTime.Now.ToString("yyyy:MM:dd,HH:mm:ss");
            reInfo.ManClerk = OperateContext.sessUserInfo.Name;
            BLLSession.Current.BD_RevDocInfoBLL.ModifyAllModel(reInfo);
            Response.Write(JsonHelper.toDefaultMsg(true, "修改成功！"));
        }

        /// <summary>
        /// 获取低级审批列表
        /// </summary>
        public void GetLowReviewList()
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
            }
            Expression<Func<View_BD_LowReviewInfo, bool>> exp = SearchExpression.FindByGroup<View_BD_LowReviewInfo>(listSearch);
            int count = 0;
            var list = BLLSession.Current.View_BD_LowReviewInfoBLL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, u => u.ID, OperateContext.Order);
            count = BLLSession.Current.View_BD_LowReviewInfoBLL.GetCount(exp);
            Response.Write(JsonHelper.toPageJson(list, count));
        }
        /// <summary>
        /// 添加低级审批级别
        /// </summary>
        public void AddLowReivew()
        {
            BD_DepRevInfo lowInfo = new BD_DepRevInfo();
            ModelTools.Form2EditModel(Request.Form, lowInfo);

            lowInfo.FillDate = DateTime.Now.ToString("yyyy:MM:dd,HH:mm:ss");
            lowInfo.ManClerk = OperateContext.sessUserInfo.Name;
            BLLSession.Current.BD_DepRevInfoBLL.Add(lowInfo);
            Response.Write(JsonHelper.toDefaultMsg(true, "添加成功！"));

        }

        /// <summary>
        /// 修改低级审批级别
        /// </summary>
        public void EditLowReivew()
        {
            int lowId = Commons.String2Int32(Request.Form["ID"]);
            BD_DepRevInfo lowInfo = BLLSession.Current.BD_DepRevInfoBLL.GetListBy(d => d.ID == lowId).FirstOrDefault();
            ModelTools.Form2EditModel(Request.Form, lowInfo);

            lowInfo.FillDate = DateTime.Now.ToString("yyyy:MM:dd,HH:mm:ss");
            lowInfo.ManClerk = OperateContext.sessUserInfo.Name;
            BLLSession.Current.BD_DepRevInfoBLL.ModifyAllModel(lowInfo);
            Response.Write(JsonHelper.toDefaultMsg(true, "保存成功！"));

        }


        public void RemoveLowReivew()
        {
            int lowId = Commons.String2Int32(Request.Form["ID"]);
            BLLSession.Current.BD_DepRevInfoBLL.DelBy(l => l.ID == lowId);
            Response.Write(JsonHelper.toDefaultMsg(true, "删除成功！"));
        }
        /// <summary>
        /// 获取高级审批列表
        /// </summary>
        public void GetHighReviewList()
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
            }
            Expression<Func<View_BD_HighReviewInfo, bool>> exp = SearchExpression.FindByGroup<View_BD_HighReviewInfo>(listSearch);
            int count = 0;
            var list = BLLSession.Current.View_BD_HighReviewInfoBLL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, u => u.DocCode, OperateContext.Order);
            count = BLLSession.Current.View_BD_HighReviewInfoBLL.GetCount(exp);
            Response.Write(JsonHelper.toPageJson(list, count));
        }

        /// <summary>
        /// 添加高级审批级别
        /// </summary>
        public void AddHighReivew()
        {
            BD_LedRevInfo lowInfo = new BD_LedRevInfo();
            ModelTools.Form2EditModel(Request.Form, lowInfo);

            lowInfo.FillDate = DateTime.Now.ToString("yyyy:MM:dd,HH:mm:ss");
            lowInfo.ManClerk = OperateContext.sessUserInfo.Name;
            BLLSession.Current.BD_LedRevInfoBLL.Add(lowInfo);
            Response.Write(JsonHelper.toDefaultMsg(true, "添加成功！"));

        }

        /// <summary>
        /// 修改高级审批级别
        /// </summary>
        public void EditHighReivew()
        {
            int highId = Commons.String2Int32(Request.Form["ID"]);
            BD_LedRevInfo highInfo = BLLSession.Current.BD_LedRevInfoBLL.GetListBy(d => d.ID == highId).FirstOrDefault();
            ModelTools.Form2EditModel(Request.Form, highInfo);

            highInfo.FillDate = DateTime.Now.ToString("yyyy:MM:dd,HH:mm:ss");
            highInfo.ManClerk = OperateContext.sessUserInfo.Name;
            BLLSession.Current.BD_LedRevInfoBLL.ModifyAllModel(highInfo);
            Response.Write(JsonHelper.toDefaultMsg(true, "保存成功！"));

        }
        public void DelHighReivew()
        {
            int Id = Convert.ToInt32(Request.Form["Id"].ToString());
            BLLSession.Current.BD_LedRevInfoBLL.DelBy(p => p.ID == Id);

            Response.Write(JsonHelper.toDefaultMsg(true, "删除成功"));
        }

        /// <summary>
        /// 获取审批类型
        /// </summary>
        public void GetReviewType()
        {
            Response.Write(JsonHelper.toJson(DALSession.Current.BD_RevDocInfoDAL.GetListBy(c => 1 == 1).Select(c => new { value = c.ID, text = c.DocName })));
        }
        /// <summary>
        /// 获取所有审核任务列表
        /// </summary>
        public void GetReviewList()
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
                if (listSearch.Where(s => s.Field == "startFillDate").FirstOrDefault() != null)
                {
                    listSearch.Where(s => s.Field == "startFillDate").FirstOrDefault().Field = "FillDate";
                }
                if (listSearch.Where(s => s.Field == "endFillDate").FirstOrDefault() != null)
                {
                    listSearch.Where(s => s.Field == "endFillDate").FirstOrDefault().Field = "FillDate";
                }
            }
            Expression<Func<View_BD_ReviewTotal, bool>> exp = SearchExpression.FindByGroup<View_BD_ReviewTotal>(listSearch);
            int count = 0;
            var list = BLLSession.Current.View_BD_ReviewTotalBLL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, u => u.FillDate, OperateContext.OrderDesc);
            count = BLLSession.Current.View_BD_ReviewTotalBLL.GetCount(exp);
            List<int> perInt = OperateContext.SessPermission.Select(p => p.Id).ToList();
            list.ForEach(r =>
            {
                if (perInt.Contains((int)r.PermissionId))
                {
                    r.CanReview = "0";
                }
                else
                {
                    r.CanReview = "1";
                }
            });

            Response.Write(JsonHelper.toPageJson(list, count));
        }
    }
}
